package com.lin.csdn.service.impl;

import com.lin.csdn.dao.impl.UserDao;
import com.lin.csdn.model.po.Messege;
import com.lin.csdn.service.intf.IUserService;
import com.lin.csdn.utils.BaseLogger;
import com.lin.csdn.utils.ResultSplit;

import java.util.ArrayList;
import java.util.List;

public class UserService implements IUserService {

    UserDao userDao=new UserDao();
    ResultSplit split=new ResultSplit();
    BaseLogger baseLogger=new BaseLogger();

    @Override
    public Messege login(String email,String password) {
        Object result = userDao.login(email);//由账号名字读取
        String[] data=result.toString().split("%");
        //判断密码是否正确
        try {
            if (data[3].equals(password)) {
                //检查账号是否被封禁
                if (data[5].equals("是]")) {
                    baseLogger.warningLog("有封禁账号尝试登录,账号名："+data[1]);
                    return new Messege("此账号已被封禁", data[1], 404);
                } else if (data[4].equals("管理员")) {
                    baseLogger.warningLog("有管理员账号尝试登录,账号名："+data[1]);
                    return new Messege("管理员登陆成功", data[1], 200);
                } else {
                    baseLogger.infoLog("用户登陆成功,用户名："+data[1]);
                    return new Messege("用户登陆成功", data[1], 200);
                }
            } else {
                if (data[5].equals("是]")) {
                    baseLogger.warningLog("有封禁账号尝试登录,账号名："+data[1]);
                    return new Messege("此账号已被封禁", data[1], 404);
                }
                baseLogger.warningLog("有账号尝试登录,但密码错误,账号名："+data[1]);
                return new Messege("密码错误", data[1], 404);
            }
        }catch (IndexOutOfBoundsException e)
        {
            baseLogger.warningLog("有未知账号尝试登录");
            return new Messege("账号错误", null, 404);
        }
    }

    @Override
    public Messege sign(String name,String email,String password){
        int id= userDao.sign(name, email, password);
        baseLogger.warningLog("有新账号注册,用户id："+id);
        return new Messege("添加成功",id,200);
    }

    @Override
    public String findName(int id){
        return userDao.findName(id);
    }

    @Override
    public int findId(String name){
        return userDao.findId(name);
    }

    @Override
    public int beFollow(String loginName){
        int loginId= userDao.findId(loginName);
        return userDao.beFollow(loginId);
    }

    @Override
    public int follow(String loginName){
        int loginId= userDao.findId(loginName);
        return userDao.follow(loginId);
    }

    //获取粉丝的用户名
    public List<Object> beFollowName(String loginName){
        int loginId= userDao.findId(loginName);
        List<Object> result= userDao.beFollowName(loginId);
        List<Object> data=new ArrayList<>();
        for (Object beFollowName : result) {
            String name = userDao.findName(Integer.parseInt(beFollowName.toString()));//读取id，并查找对应的名字
            data.add(name);
        }
        return data;
    }


    //获取关注的用户名
    public List<Object> followName(String loginName){
        int loginId= userDao.findId(loginName);
        List<Object> result=userDao.followName(loginId);
        List<Object> data=new ArrayList<>();
        for (Object followName : result) {
            String name = userDao.findName(Integer.parseInt(followName.toString()));//读取id，并查找对应的名字
            data.add(name);
        }
        return data;
    }

    @Override
    public List<Object> searchAll(int beginIndex,int pageSize){
        List<Object> result=userDao.searchAll(beginIndex,pageSize);
        return split.resultSplit(result);
    }

    @Override
    public void banUser(String userId){
        userDao.banUser(Integer.parseInt(userId.replace(" ","")));
    }

    public List<Object> searchInformation(String userName){
        int userid=userDao.findId(userName);
        Object result=userDao.searchInformation(userid);
        return split.resultSplit(result);
    }

    //更新邮箱信息
    public void updateEmail(String userName,String email){
        int userid=userDao.findId(userName);
        userDao.updateEmail(userid,email);
    }

    //更新用户名
    public void updateName(String userName,String name){
        int userid=userDao.findId(userName);
        userDao.updateName(userid,name);
    }
}
